#include "StdAfx.h"
#include "tribe_chat_mgr.h"
#include "im_packet_impl.h"
#include "im_helper.h"

tribe_chat_mgr::tribe_chat_mgr()
{
}

tribe_chat_mgr::~tribe_chat_mgr()
{
}

bool 
tribe_chat_mgr::insert(const char* tribe_id, HWND wnd, const char* call_id)
{
	if( NULL == tribe_id || tribe_id[0] == 0 )
	{
		if( wnd )
		{
			::PostMessage(wnd, WM_CLOSE, 0, 0);
		}
		assert(0);
		return false;
	}

	auto_lock_type __mon(this->mutex_);

	chat_info_t* p = &this->map_[tribe_id];
	/// 
	if( p->wnd && p->wnd != wnd && wnd )
	{
		::PostMessage(wnd, WM_CLOSE, 0, 0);
		return false;
	}

	if( NULL == p->wnd && wnd )
	{
		p->wnd = wnd;
	}

	if( call_id && call_id[0] )
	{
		if( p->call_id_lst.end() == std::find(p->call_id_lst.begin(), p->call_id_lst.end(), call_id) )
		{
			p->call_id_lst.push_back(call_id);
		}
	}
	return true;
}

void 
tribe_chat_mgr::remove_call_id(const char* tribe_id, const char* call_id)
{
	if( NULL == tribe_id || tribe_id[0] == 0 || NULL == call_id )
	{
		assert(0);
		return;
	}

	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.find(tribe_id);
	if( it == this->map_.end() )
	{
		return;
	}

	chat_info_t& info = it->second;
	info.call_id_lst.remove(call_id);

	if( info.call_id_lst.size() == 0 )
	{
		this->map_.erase(it);
	}
}

void 
tribe_chat_mgr::remove_all(bool destroy_wnd)
{
	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.begin();
	for( ; it != this->map_.end(); )
	{
		if( destroy_wnd )
		{
			HWND h = it->second.wnd;
			this->map_.erase(it++);
			if( h )
			{
				::PostMessage(h, WM_CLOSE, 0, 0);
				/// ::DestroyWindow(h);
			}
		}
		else
		{
			this->map_.erase(it++);
		}
	}
}

std::string 
tribe_chat_mgr::find_call_id_and_add(const char* tribe_id)
{
	std::string s("");

	if( NULL == tribe_id || tribe_id[0] == 0 )
	{
		assert(0);
		return s;
	}

	auto_lock_type __mon(this->mutex_);

	chat_info_t* info = &this->map_[tribe_id];
	if( info->call_id_lst.size() < 1 )
	{
		s = im_helper::make_call_id();
		info->call_id_lst.push_back(s);
	}
	else
	{
		s = *info->call_id_lst.begin();
	}

	return s;
}

std::string
tribe_chat_mgr::find_call_id(const char* tribe_id)
{
	std::string s("");

	if( NULL == tribe_id || tribe_id[0] == 0 )
	{
		assert(0);
		return s;
	}

	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.find(tribe_id);

	if( it != this->map_.end() )
	{
		if( it->second.call_id_lst.size() > 0 )
		{
			s = *(it->second.call_id_lst.begin());
		}
	}

	return s;
}

HWND 
tribe_chat_mgr::find_wnd(const char* tribe_id)
{
	if( NULL == tribe_id || tribe_id[0] == 0 )
	{
		assert(0);
		return NULL;
	}

	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.find(tribe_id);
	if( it == this->map_.end() )
	{
		return NULL;
	}
	return it->second.wnd;
}

bool 
tribe_chat_mgr::set_wnd(const char* tribe_id, HWND wnd)
{
	if( NULL == tribe_id || tribe_id[0] == 0 )
	{
		assert(0);
		return false;
	}

	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.find(tribe_id);
	if( it == this->map_.end() )
	{
		assert(0);
		return false;
	}
	it->second.wnd = wnd;
	return true;
}

bool 
tribe_chat_mgr::have_opened_wnd()
{
	auto_lock_type __mon(this->mutex_);

	map_type::iterator it = this->map_.begin();
	for( ; it != this->map_.end(); ++it )
	{
		if( it->second.wnd )
		{
			return true;
		}
	}
	return false;
}